/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Controls.Material 2.4
import QtQuick.Layouts 1.11
import QtQuick.Window 2.11

ItemDelegate {
    id: root
    width: parent.width
    checkable: true

    property int lastKey: 0

    Timer {
        id: timer
    }
    function delay(delayTime, cb) {
        timer.interval = delayTime;
        timer.repeat = false;
        timer.triggered.connect(cb);
        timer.start();
    }

    contentItem: ColumnLayout {
        spacing: 0

        RowLayout {
            Switch {
                text: "开关"
                checked: model.enable
                onClicked: model.enable = checked
            }
            Item {
                width: 15
            }
            Label {
                id: start
                text: "连击键："
            }
            TextField {
                id: startTextField
                horizontalAlignment: Text.AlignHCenter
                Layout.maximumWidth: 70
                text: model.startKey
                font.capitalization: Font.AllUppercase
                Keys.onPressed: {
                    startTextField.text = ""
                    delay(20, function() {
                        switch(event.key)
                        {
                        case Qt.Key_F1:
                            startTextField.text = "F1"
                            break
                        case Qt.Key_F2:
                            startTextField.text = "F2"
                            break
                        case Qt.Key_F3:
                            startTextField.text = "F3"
                            break
                        case Qt.Key_F4:
                            startTextField.text = "F4"
                            break
                        case Qt.Key_F5:
                            startTextField.text = "F5"
                            break
                        case Qt.Key_F6:
                            startTextField.text = "F6"
                            break
                        case Qt.Key_F7:
                            startTextField.text = "F7"
                            break
                        case Qt.Key_F8:
                            startTextField.text = "F8"
                            break
                        case Qt.Key_F9:
                            startTextField.text = "F9"
                            break
                        case Qt.Key_F10:
                            startTextField.text = "F10"
                            break
                        case Qt.Key_F11:
                            startTextField.text = "F11"
                            break
                        case Qt.Key_F12:
                            startTextField.text = "F12"
                            break
                        default:
                            startTextField.text = event.text
                        }
                        if(startTextField.text != "")
                        {
                            model.startKey = startTextField.text
                        }
                    })
                }
            }
            Item {
                width: 118
            }
            Label {
                text: "重复次数："
            }
            TextField {
                id: stopTextField
                horizontalAlignment: Text.AlignHCenter
                Layout.maximumWidth: 70
                text: model.stopKey
                onTextEdited: model.stopKey = text
                validator: IntValidator{ bottom: 0;top:99999}
            }
            Label {
                text: "按键间隔："
            }
            TextField {
                id: intervalTextField
                horizontalAlignment: Text.AlignHCenter
                Layout.maximumWidth: 50 
                text: model.interval
                onTextEdited: model.interval = text
                validator: IntValidator{ bottom: 0;top:99999}
            }
            Label {
                text: "ms"
            }
            Item {
                width: 30
            }
            Button {
                id: deleteAlarmButton
                text: qsTr("删除")
                width: 40
                height: 40
                onClicked: root.ListView.view.model.remove(index)
            }
        }
    }
}
